<template>
  <div class="main">
    <li v-for="(item,index) in newsList" :key="index" @click="toChat(item)">
      <div class="icon">
        <img :src="item.img" alt>
      </div>
      <div class="rightBox">
        <div class="t">
          {{item.name}}
          <div class="time">{{item.time}}</div>
        </div>
        <div class="b">{{item.txt}}</div>
      </div>
    </li>
  </div>
</template>
<script>
import { chatroomList } from "../../../api";
import { getIMData } from "../../../im";
import { formatDateTime } from "../../../utils/common";
export default {
  name: "news",

  data() {
    getIMData(data => {
      this.users = data.users;
    });
    return {
      newsList: [
        // {
        //   img: require("../../../assets/img/003.png"),
        //   name: "刘小光字 消息",
        //   txt: "热门一个游戏，进来组队玩一把......",
        //   time: "下午15:26"
        // }
      ]
    };
  },
  created() {
    this.reload();
  },

  methods: {
    toChat(item) {
      this.$router.push({
        path: "/coze",
        query: { item: JSON.stringify(item) }
      });
    },

    reload() {
      //获取会话列表
      let sessionArray = [];
      let session = localStorage.getItem(
        localStorage.zyUserId + "_" + "session"
      );
      if (session) {
        session = JSON.parse(session);
        for (let key in session) {
          sessionArray.push(session[key]);
        }
      }
      //排序
      sessionArray = sessionArray.sort(function(a, b) {
        return b.time - a.time;
      });
      let sessionShowArray = [];
      for (let key in sessionArray) {
        let sessionObject = JSON.parse(sessionArray[key]);
        let nick = sessionObject.fromNick;
        let account = "";
        if (sessionObject.flow == "out") {
          //数据里没得，要去玩家列表里找
          for (key in this.users) {
            if (this.users[key].account == sessionObject.target) {
              nick = this.users[key].nick;
            }
          }
          account = sessionObject.target;
        } else if (sessionObject.flow == "in") {
          account = sessionObject.from;
        }
        let session = {
          img: require("../../../assets/img/003.png"),
          name: nick + "的消息",
          txt: sessionObject.text,
          time: formatDateTime(sessionObject.time / 1000),
          account: account,
          nick: nick
        };
        sessionShowArray.push(session);
      }
      this.newsList = sessionShowArray;
    }
  }
};
</script>
<style lang="stylus" scoped>
.main {
  width: 100%;
  background: #FFF;

  li {
    width: 6.84rem;
    border-bottom: 1px solid #E6E6E6;
    margin: 0 auto;
    display: flex;

    .icon {
      width: 1.29rem;
      height: 1.31rem;
      margin: 10px 0;
      flex: 0 1 1.29rem;

      img {
        width: 100%;
        height: 100%;
      }
    }

    .rightBox {
      padding-top: 10px;
      flex: 1;
      margin-left: 10px;

      div {
        line-height: 0.7rem;
      }

      .t {
        font-size: 16px;

        .time {
          font-size: 13px;
          color: #595959;
          float: right;
        }
      }

      .b {
        font-size: 13px;
        color: #595959;
      }
    }
  }
}
</style>

